#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# make monomial key

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

# make monovalent key

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monovalent-key -monovalent-key $output_dir/monovalent_key_file.xf -polyvalent-key $output_dir/polyvalent_key_file.xf -polyvalent-proof $output_dir/polyvalent_proof_file.xf -start-timestamp $(date +%s) -end-timestamp $(expr $(date +%s) + 100)
"
echo ""; echo $str; echo ""
eval $str

# make symmetric_certificate

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-certificate-symmetric -subject-monovalent-key $output_dir/monovalent_key_file.xf -subject-str \"Alice monovalent key\" -symmetric-certificate $output_dir/symmetric_certificate_file.xf -start-timestamp $(date +%s) -end-timestamp $(expr $(date +%s) + 100)
"
echo ""; echo $str; echo ""
eval $str

# check certificate

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/monomial_key_file.xf -sym-certificate $output_dir/symmetric_certificate_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/lib_info_log_file.xf \
    --check-certificate-symmetric
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_log_file.xf ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_log_file.xf\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# generate a wrong monomial key

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/wrong_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str


# check certificate

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/wrong_monomial_key_file.xf -sym-certificate $output_dir/symmetric_certificate_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/wrong_lib_info_log_file.xf \
    --check-certificate-symmetric
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/wrong_lib_info_log_file.xf ]; then echo FAIL ; exit ; fi ; \
if grep -q PASS \"$output_dir/wrong_lib_info_log_file.xf\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# check certificate content

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -sym-certificate $output_dir/symmetric_certificate_file.xf \
    --permutation-conf -arch $architecture \
    --convert-symmetric-certificate-to-monovalent-key -monovalent-key $output_dir/certificate_monovalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/original_monovalent_key.json \
    --print-header -xrn-file $output_dir/monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/certificate_monovalent_key.json \
    --print-header -xrn-file $output_dir/certificate_monovalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/certificate_monovalent_key.json $output_dir/original_monovalent_key.json && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

